package com.example.administrator.questionanswer.adapter;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.administrator.questionanswer.R;
import com.example.administrator.questionanswer.greendao.AnswerBean;
import com.example.administrator.questionanswer.utils.MyTextView;

import java.text.ParseException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2018/4/6 0006.
 */

public class AnswerShowAdapter extends BaseAbstactRecycleAdapter<AnswerBean, AnswerShowAdapter.AnswerShowAdapterViewHolder> {

    Context context;

    HashMap<Integer, String> map;

    public AnswerShowAdapter(Context context) {
        this.context = context;
        map = new HashMap<>();
    }

    String[] split;

    @Override
    public void getViewHolder(AnswerShowAdapterViewHolder baseViewHolder, int position, List<AnswerBean> mList) throws ParseException {

        AnswerBean answerBean = mList.get(position);
        String questionAContent = answerBean.getQuestionAContent();
        String questionBContent = answerBean.getQuestionBContent();
        String questionCContent = answerBean.getQuestionCContent();
        String questionDContent = answerBean.getQuestionDContent();
        String questionEContent = answerBean.getQuestionEContent();
        String questionFContent = answerBean.getQuestionFContent();
        String questionName = answerBean.getQuestionName();
        String questionContent = answerBean.getQuestionContent();
        Long questionId = answerBean.getQuestionId();
        baseViewHolder.txtQuestionName.setText(questionId + "." + questionName);

        if (questionAContent != null) {
            baseViewHolder.txtQuestionA.setVisibility(View.VISIBLE);
            baseViewHolder.txtQuestionA.setText("A、" + questionAContent);
        } else {
            baseViewHolder.txtQuestionA.setVisibility(View.GONE);
        }

        if (questionBContent != null) {
            baseViewHolder.txtQuestionB.setVisibility(View.VISIBLE);
            baseViewHolder.txtQuestionB.setText("B、" + questionBContent);
        } else {
            baseViewHolder.txtQuestionB.setVisibility(View.GONE);
        }

        if (questionCContent != null) {
            baseViewHolder.txtQuestionC.setVisibility(View.VISIBLE);
            baseViewHolder.txtQuestionC.setText("C、" + questionCContent);
        } else {
            baseViewHolder.txtQuestionC.setVisibility(View.GONE);
        }

        if (questionDContent != null) {
            baseViewHolder.txtQuestionD.setVisibility(View.VISIBLE);
            baseViewHolder.txtQuestionD.setText("D、" + questionDContent);
        } else {
            baseViewHolder.txtQuestionD.setVisibility(View.GONE);
        }
        if (questionEContent != null) {
            baseViewHolder.txtQuestionE.setVisibility(View.VISIBLE);
            baseViewHolder.txtQuestionE.setText("E、" + questionEContent);
        } else {
            baseViewHolder.txtQuestionE.setVisibility(View.GONE);
        }

        if (questionFContent != null) {
            baseViewHolder.txtQuestionF.setVisibility(View.VISIBLE);
            baseViewHolder.txtQuestionF.setText("F、" + questionFContent);
        } else {
            baseViewHolder.txtQuestionF.setVisibility(View.GONE);
        }
        Drawable drawable = context.getResources().getDrawable(R.drawable.ic_radio_button_unchecked_black_24dp);
        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
        baseViewHolder.txtQuestionA.setCompoundDrawables(drawable, null, null, null);
        baseViewHolder.txtQuestionB.setCompoundDrawables(drawable, null, null, null);
        baseViewHolder.txtQuestionC.setCompoundDrawables(drawable, null, null, null);
        baseViewHolder.txtQuestionD.setCompoundDrawables(drawable, null, null, null);
        baseViewHolder.txtQuestionE.setCompoundDrawables(drawable, null, null, null);
        baseViewHolder.txtQuestionF.setCompoundDrawables(drawable, null, null, null);

        if (questionContent.length() > 1) {
            String[] split = questionContent.split(",");
            for (int i = 0; i < split.length; i++) {
                if (split[i].equals("A")) {
                    Drawable drawable1 = context.getResources().getDrawable(R.drawable.ic_radio_button_checked_black_24dp);
                    drawable1.setBounds(0, 0, drawable1.getMinimumWidth(), drawable1.getMinimumHeight());
                    if (baseViewHolder.txtQuestionA.getVisibility() == View.VISIBLE) {
                        baseViewHolder.txtQuestionA.setCompoundDrawables(drawable1, null, null, null);
                    }
                } else if (split[i].equals("B")) {
                    Drawable drawable1 = context.getResources().getDrawable(R.drawable.ic_radio_button_checked_black_24dp);
                    drawable1.setBounds(0, 0, drawable1.getMinimumWidth(), drawable1.getMinimumHeight());
                    if (baseViewHolder.txtQuestionB.getVisibility() == View.VISIBLE) {
                        baseViewHolder.txtQuestionB.setCompoundDrawables(drawable1, null, null, null);
                    }
                } else if (split[i].equals("C")) {
                    Drawable drawable1 = context.getResources().getDrawable(R.drawable.ic_radio_button_checked_black_24dp);
                    drawable1.setBounds(0, 0, drawable1.getMinimumWidth(), drawable1.getMinimumHeight());
                    if (baseViewHolder.txtQuestionC.getVisibility() == View.VISIBLE) {
                        baseViewHolder.txtQuestionC.setCompoundDrawables(drawable1, null, null, null);
                    }
                } else if (split[i].equals("D")) {
                    Drawable drawable1 = context.getResources().getDrawable(R.drawable.ic_radio_button_checked_black_24dp);
                    drawable1.setBounds(0, 0, drawable1.getMinimumWidth(), drawable1.getMinimumHeight());
                    if (baseViewHolder.txtQuestionD.getVisibility() == View.VISIBLE) {
                        baseViewHolder.txtQuestionD.setCompoundDrawables(drawable1, null, null, null);
                    }
                } else if (split[i].equals("E")) {
                    Drawable drawable1 = context.getResources().getDrawable(R.drawable.ic_radio_button_checked_black_24dp);
                    drawable1.setBounds(0, 0, drawable1.getMinimumWidth(), drawable1.getMinimumHeight());
                    if (baseViewHolder.txtQuestionE.getVisibility() == View.VISIBLE) {
                        baseViewHolder.txtQuestionE.setCompoundDrawables(drawable1, null, null, null);
                    }
                } else if (split[i].equals("F")) {
                    Drawable drawable1 = context.getResources().getDrawable(R.drawable.ic_radio_button_checked_black_24dp);
                    drawable1.setBounds(0, 0, drawable1.getMinimumWidth(), drawable1.getMinimumHeight());
                    if (baseViewHolder.txtQuestionF.getVisibility() == View.VISIBLE) {
                        baseViewHolder.txtQuestionF.setCompoundDrawables(drawable1, null, null, null);
                    }
                }
            }


        } else {
            if (questionContent.equals("A")) {
                Drawable drawable1 = context.getResources().getDrawable(R.drawable.ic_radio_button_checked_black_24dp);
                drawable1.setBounds(0, 0, drawable1.getMinimumWidth(), drawable1.getMinimumHeight());
                if (baseViewHolder.txtQuestionA.getVisibility() == View.VISIBLE) {
                    baseViewHolder.txtQuestionA.setCompoundDrawables(drawable1, null, null, null);
                }
            } else if (questionContent.equals("B")) {
                Drawable drawable1 = context.getResources().getDrawable(R.drawable.ic_radio_button_checked_black_24dp);
                drawable1.setBounds(0, 0, drawable1.getMinimumWidth(), drawable1.getMinimumHeight());
                if (baseViewHolder.txtQuestionB.getVisibility() == View.VISIBLE) {
                    baseViewHolder.txtQuestionB.setCompoundDrawables(drawable1, null, null, null);
                }
            } else if (questionContent.equals("C")) {
                Drawable drawable1 = context.getResources().getDrawable(R.drawable.ic_radio_button_checked_black_24dp);
                drawable1.setBounds(0, 0, drawable1.getMinimumWidth(), drawable1.getMinimumHeight());
                if (baseViewHolder.txtQuestionC.getVisibility() == View.VISIBLE) {
                    baseViewHolder.txtQuestionC.setCompoundDrawables(drawable1, null, null, null);
                }
            } else if (questionContent.equals("D")) {
                Drawable drawable1 = context.getResources().getDrawable(R.drawable.ic_radio_button_checked_black_24dp);
                drawable1.setBounds(0, 0, drawable1.getMinimumWidth(), drawable1.getMinimumHeight());
                if (baseViewHolder.txtQuestionD.getVisibility() == View.VISIBLE) {
                    baseViewHolder.txtQuestionD.setCompoundDrawables(drawable1, null, null, null);
                }
            } else if (questionContent.equals("E")) {
                Drawable drawable1 = context.getResources().getDrawable(R.drawable.ic_radio_button_checked_black_24dp);
                drawable1.setBounds(0, 0, drawable1.getMinimumWidth(), drawable1.getMinimumHeight());
                if (baseViewHolder.txtQuestionE.getVisibility() == View.VISIBLE) {
                    baseViewHolder.txtQuestionE.setCompoundDrawables(drawable1, null, null, null);
                }
            } else if (questionContent.equals("F")) {
                Drawable drawable1 = context.getResources().getDrawable(R.drawable.ic_radio_button_checked_black_24dp);
                drawable1.setBounds(0, 0, drawable1.getMinimumWidth(), drawable1.getMinimumHeight());
                if (baseViewHolder.txtQuestionF.getVisibility() == View.VISIBLE) {
                    baseViewHolder.txtQuestionF.setCompoundDrawables(drawable1, null, null, null);
                }
            }
        }


    }

    @Override
    public AnswerShowAdapterViewHolder creatHolder(ViewGroup parent) {

        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_question_show, parent, false);
        return new AnswerShowAdapterViewHolder(view);
    }

    static class AnswerShowAdapterViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.txt_question_name)
        MyTextView txtQuestionName;
        @BindView(R.id.txt_question_a)
        TextView txtQuestionA;
        @BindView(R.id.txt_question_a_percent)
        TextView txtQuestionAPercent;
        @BindView(R.id.txt_question_b)
        TextView txtQuestionB;
        @BindView(R.id.txt_question_b_percent)
        TextView txtQuestionBPercent;
        @BindView(R.id.txt_question_c)
        TextView txtQuestionC;
        @BindView(R.id.txt_question_c_percent)
        TextView txtQuestionCPercent;
        @BindView(R.id.txt_question_d)
        TextView txtQuestionD;
        @BindView(R.id.txt_question_d_percent)
        TextView txtQuestionDPercent;
        @BindView(R.id.txt_question_e)
        TextView txtQuestionE;
        @BindView(R.id.txt_question_e_percent)
        TextView txtQuestionEPercent;
        @BindView(R.id.txt_question_f)
        TextView txtQuestionF;
        @BindView(R.id.txt_question_f_percent)
        TextView txtQuestionFPercent;

        public AnswerShowAdapterViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
